package com.eqdd.yiqidian.adapter.simple;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.eqdd.multitree.tree.bean.Node;
import com.eqdd.multitree.tree.bean.TreeListViewAdapter;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.bean.rebuild.MultiTreeBean;

import java.util.ArrayList;
import java.util.List;


public class SimpleTreeAdapter extends TreeListViewAdapter<MultiTreeBean> {

    private List<MultiTreeBean> datas;
    private List<MultiTreeBean> dataOrigin = new ArrayList<>();

    public SimpleTreeAdapter(ListView mTree, Context context, List<MultiTreeBean> datas,
                             int defaultExpandLevel) throws IllegalArgumentException,
            IllegalAccessException {
        super(mTree, context, datas, defaultExpandLevel);
        this.datas = datas;
        this.dataOrigin.addAll(datas);
    }

    @Override
    public View getConvertView(Node node, int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.list_item, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.icon = convertView
                    .findViewById(R.id.id_treenode_icon);
            viewHolder.label = convertView
                    .findViewById(R.id.id_treenode_label);
            viewHolder.checkBox = convertView
                    .findViewById(R.id.checkbox);
            viewHolder.view = convertView
                    .findViewById(R.id.view);
            convertView.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        if (node.getIcon() == -1) {
            viewHolder.icon.setVisibility(View.INVISIBLE);
        } else {
            viewHolder.icon.setVisibility(View.VISIBLE);
            viewHolder.icon.setImageResource(node.getIcon());
        }

        viewHolder.checkBox.setChecked(datas.get(position).isSelect());
        viewHolder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                datas.get(position).setSelect(!datas.get(position).isSelect());
                if (!node.isLeaf()) {
                    setChildChecked(datas.get(position).getPid(), position, datas.get(position).isSelect());
                    setChildCheckedOrigin(datas.get(position).getPid(), position, datas.get(position).isSelect());
                }
                notifyDataSetChanged();
            }
        });
        viewHolder.label.setText(node.getName());


        return convertView;
    }
    public List<MultiTreeBean> getOriginData() {
        return dataOrigin;
    }

    public void collapse(int position) {
        int pid = datas.get(position).getPid();
        int endPosition = -1;
        for (int i = position + 1, lenth = datas.size(); i < lenth; i++) {
            if (datas.get(i).getPid() == pid) {
                endPosition = i - 1;
                break;
            }
        }
        if (endPosition == -1) {
            return;
        }
        for (int i = endPosition; i >= position + 1; i--) {
            datas.remove(i);
        }
    }

    public void expand(int position) {
        int pid = datas.get(position).getPid();
        int id = datas.get(position).getId();
        int i1 = dataOrigin.indexOf(datas.get(position)) + 1;
        int startPosition = position + 1;
        for (int lenth = dataOrigin.size(); i1 < lenth; i1++) {

            if (dataOrigin.get(i1).getPid() != pid) {
                if (dataOrigin.get(i1).getPid() == id) {
                    datas.add(startPosition, dataOrigin.get(i1));
                    startPosition++;
                }
            } else {
                break;
            }
        }
//            for (int i = position + 1, lenth = dataOrigin.size(); i < lenth; i++, i1++) {
//                if (dataOrigin.get(i1).getPid() != pid && dataOrigin.get(i1).getId() == id) {
//                    datas.add(i, dataOrigin.get(i1));
//                } else {
//                    break;
//                }
//            }
    }

    @Override
    public void expandOrCollapse(int position) {
        Node n = this.mNodes.get(position);
        if (n != null && !n.isLeaf()) {
            if (n.isExpand()) {
                collapse(position);
            } else {
                expand(position);
            }
        }
        super.expandOrCollapse(position);
    }

    private void setChildChecked(int pid, int position, boolean isChecked) {
        for (int i = position + 1, lenth = datas.size(); i < lenth; i++) {
            if (datas.get(i).getPid() != pid) {
                datas.get(i).setSelect(isChecked);
            } else {
                break;
            }
        }
    }

    private void setChildCheckedOrigin(int pid, int position, boolean isChecked) {
        int i = dataOrigin.indexOf(datas.get(position)) + 1;
        for (int lenth = dataOrigin.size(); i < lenth; i++) {
            if (dataOrigin.get(i).getPid() != pid) {
                dataOrigin.get(i).setSelect(isChecked);
            } else {
                break;
            }
        }
    }

    private final class ViewHolder {
        ImageView icon;
        TextView label;
        CheckBox checkBox;
        View view;
    }

}
